package com.taxi.driver.module.order.price;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qianxx.utils.PhoneUtils;
import com.qianxx.utils.TypeUtils;
import com.qianxx.view.HeadView;
import com.taxi.driver.R;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.config.BizStatus;
import com.taxi.driver.config.IConstants;
import com.taxi.driver.event.OrderEvent;
import com.taxi.driver.module.order.price.dagger.DaggerPriceInputComponent;
import com.taxi.driver.module.order.price.dagger.PriceInputModule;
import com.taxi.driver.module.vo.PassengerVO;
import com.taxi.driver.socket.SocketEvent;
import com.taxi.driver.util.PointFilter;
import com.taxi.driver.widget.ImageViewPlus;
import com.taxi.driver.widget.dialog.TwoSelectorDialog;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;

import static org.litepal.LitePalApplication.getContext;

/**
 * 功能描述：(出租车司机)输入金额页
 */
public class PriceInputActivity extends BaseActivity implements PriceInputContract.View {

    public static void actionStart(Context context, String orderUuid, PassengerVO passengerVO) {
        Intent intent = new Intent(context, PriceInputActivity.class);
        intent.putExtra(IConstants.ORDER_UUID, orderUuid);
        if (passengerVO != null) intent.putExtra(IConstants.PARAMS, passengerVO);
        context.startActivity(intent);
    }

    @BindView(R.id.head_view)
    HeadView mHeadView;
    @BindView(R.id.iv_avatar)
    ImageView mIvAvatar;
    @BindView(R.id.tv_info)
    TextView mTvInfo;
    @BindView(R.id.iv_phone)
    ImageViewPlus mIvPhone;
    @BindView(R.id.et_price)
    EditText mEtPrice;
    @BindView(R.id.tv_check)
    TextView mTvCheck;
    @BindView(R.id.tv_received_cash)
    TextView mTvReceivedCash;

    @Inject
    PriceInputPresenter mPresenter;

    PassengerVO mPassengerVO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_price_input);
        ButterKnife.bind(this);

        DaggerPriceInputComponent.builder()
                .appComponent(getAppComponent())
                .priceInputModule(new PriceInputModule(this))
                .build()
                .inject(this);

        mHeadView.setLeftVisibility(false); //隐藏返回键
        mEtPrice.setFilters(new InputFilter[]{new PointFilter(1, 10000)});
        mPresenter.setOrderUuid(getIntent().getStringExtra(IConstants.ORDER_UUID)); //设置订单编号
        Serializable passengerVO = getIntent().getSerializableExtra(IConstants.PARAMS);
        if (passengerVO != null) {
            mPassengerVO = (PassengerVO) passengerVO;
        }
        showPassengerInfo(mPassengerVO); //显示乘客信息
    }

    @OnTextChanged(value = {R.id.et_price})
    public void onText(CharSequence s) {
        // 控制按键是否可以点击
        mTvCheck.setEnabled(s.length() > 0);
        mTvReceivedCash.setEnabled(s.length() <= 0);
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void onBackPressed() {

    }

    @OnClick({R.id.iv_phone, R.id.tv_check, R.id.tv_received_cash})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_phone: //拨号
                PhoneUtils.skip(getContext(), mPassengerVO == null ? "" : TypeUtils.getValue(mPassengerVO.mobile));
                break;
            case R.id.tv_check: //确认金额
                mPresenter.updateFare(mEtPrice.getEditableText().toString().trim());
                break;
            case R.id.tv_received_cash:
                new TwoSelectorDialog(this, null, "确认已收到乘客支付的现金？")
                        .setCancelClickListener(Dialog::dismiss)
                        .setConfirmClickListener(dialog -> {
                            mPresenter.receiveCash();
                            dialog.dismiss();
                        })
                        .show();
                break;
        }
    }

    @Override
    public void showPassengerInfo(PassengerVO vo) {
        if (vo == null) return;
        if (!TextUtils.isEmpty(vo.face)) Glide.with(this).load(vo.face).into(mIvAvatar);
        String phoneEnd = vo.getPassengerPhoneEnd();
        mTvInfo.setText(TextUtils.isEmpty(phoneEnd) ? "" : "尾号" + phoneEnd);
    }

    @Override
    public void updateFareSuccess(String orderUuid, double price) {
        // 营运状态（空驶）
        EventBus.getDefault().post(new SocketEvent(SocketEvent.BIZ_STATUS, BizStatus.EMPTY_DRIVING));
        // 发送保存的金额，在OrderDetailPresenter中处理
        EventBus.getDefault().post(new OrderEvent(OrderEvent.TAXI_UPDATE_FARE, orderUuid, price));
        finish();
    }

    @Override
    public void cashSuccess() {
        // 营运状态（空驶）
        EventBus.getDefault().post(new SocketEvent(SocketEvent.BIZ_STATUS, BizStatus.EMPTY_DRIVING));
        finish();
    }
}
